#ifndef __SQLCLN_H__
    #define __SQLCLN_H__

    #include "sqlcpp.h"

    namespace McoSql
    {

        class TcpIpSocket;
        class DataSourceStub;

        /**
         * Client interface for sending SQL requests to the server
         */
        class RemoteSqlEngine: public SqlEngine
        {
                TcpIpSocket* socket;
                size_t txBufSize;
                char* txBuf;
                size_t dataSourceMark;
                DataSourceStub* activeDataSource;

                static void responseReader(DataSourceStub* ds, void* arg);
                static void responseDestructor(DataSourceStub* ds, void* arg);
                String* readResponse();

            public:
                /**
                 * Prepared queries are not supported
                 */
                void prepare(PreparedStatement &stmt, char* sql, ...);

                /**
                 * Remotely execute query at server
                 */
                DataSource* vexecute(Transaction* trans, char const* sql, va_list* list, Value** array, size_t
                                     &nRecords);

                /**
                 * Open connection with server
                 * @param hostname name of the where server is located
                 * @param port server port
                 * @param maxConnectAttempts maximal attempts of connection to server, interval between 
                 * each connection attempt is one second, so totally connection will be performed
                 * during maxConnectionAttempts seconds
                 * @return <code>true</code> if connection successfully established, <code>false</code> otherwise
                 */
                bool open(char const* hostname, int port, int maxConnectAttempts = 10);

                /**
                 * Close connection with server
                 */
                void close();

                /**
                 * Check if engine is local or remote
                 */
                virtual bool isRemote();
            
                /** Constructor of SQL engine remote interface
                 * @param txBufSize size of transmit buffer (used to serialize requests to be sent to the server)
                 */
                RemoteSqlEngine(size_t txBufSize = 64 * 1024);

                /**
                 * Destructor of SQL engine remote interface
                 */
                ~RemoteSqlEngine();
        };
    }

#endif
